<template>
  <div>
    <van-nav-bar
      title="账号登录"
      left-text=""
      left-arrow
      @click-left="$router.go(-1)"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请填写账号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
        <router-link class="link" to="/register"
          >还没有账号，去注册~</router-link
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
// import { setToken } from '@/utils/storage'
import {setToken} from '@/utils/storage'
import {setName} from '@/utils/storage'
export default {
  components: {},
  props: {},
  data() {
    return {
      username: '',
      password: '',
    }
  },
  computed: {},
  watch: {},
  created() {},
  // methods: {
  //   // async onSubmit(value) {
  //   //   // const res = await login(data)
  //   //   // // this.$toast.success('登录成功')
  //   //   // // this.$router.push('/index')
  //   //   // console.log(res)
  //   //   const { data } = await login(value)
  //   // setToken(data.token)
  //   // this.$toast.success('登录成功')
  //   // // this.$router.push('/index')
  //   // },
  //    async onSubmit (values) {
  //   const { data } = await login(values)
  //   setToken(data.token)
  //   this.$toast.success('登录成功')
  //   this.$router.push('/')
  // }
  // },
  methods: {
  async onSubmit (value) {
    //  console.log('submit', value);
    const {body} = await login(value)
    //存token
    setToken(body.token)
    //存名字
    setName(this.username)
    console.log(body);
    this.$toast.success('登录成功')
    this.$router.push('/my')
  }
}
}
</script>

<style scoped>
.link {
  display: block;
  color: #069;
  font-size: 16px;
  margin: 15px auto;
  text-align: center;
}
</style>
